class Node{
  constructor(el){
      this.el = el;
      this.next = null;
  }
}
class linkedList{
  constructor(){
      this.head = new Node("head");
  }
  //查找链表中指定元素
  find(item){
      let currNode = this.head;
      while(currNode != null && currNode.el != item){
          currNode = currNode.next;
      }
      return currNode;
  }
  //查找指定元素的前驱节点
  findPrevious(item){
      let currNode = this.head;
      while(currNode.next != null && currNode.next.el != item){
          currNode = currNode.next;
      }
      return currNode;
  }

  insert(newEl,item){
      let newNode = new Node(newEl);
      let current = this.find(item);
      //元素不存在，返回false
      if( current == null ) return false;
      newNode.next = current.next;
      current.next = newNode;
      return true;
  }
  remove(item){
      let prevNode = this.findPrevious(item);
      //元素不存在，返回false
      if(prevNode.next == null) return false;
      prevNode.next = prevNode.next.next;
      return true;
  }
  toString(){
      let currNode = this.head;
      let str = ''
      while(currNode.next != null){
          str = str + currNode.next.el + ' -> ';
          currNode = currNode.next;
      }
      return str.slice(0,str.length-4);
  }
}

let link = new linkedList();
link.insert("张三","head");
link.insert("李四","张三");
link.insert("王五","李四");
link.insert("赵六","王五");
console.log(link.toString());      //张三 -> 李四 -> 王五 -> 赵六
link.remove("王五");
console.log(link.toString());      //张三 -> 李四 -> 赵六